def quickselect(A):
    n=len(A)
    k=n//2
    arr=A.copy()
    left,right=0,n-1
    while left<right:
        pivot=arr[(left+right)//2]
        i,j=left,right
        while True:
            while arr[i]<pivot:i+=1
            while arr[j]>pivot:j-=1
            if i>=j:break
            arr[i],arr[j]=arr[j],arr[i]
            i+=1
            j-=1
        if k<=j:
            right=j
        else:
            left=j+1
    A1=arr[:k]
    A2=arr[k:]
    return A1,A2
if __name__=="__main__":
    A=[6,3,14,2,8,9,2,1,7,4]
    A1,A2=quickselect(A)
    print("A1:",A1)
    print("A2:",A2)
    print("|n1-n2|=",abs(len(A1)-len(A2)))
    s1,s2=sum(A1),sum(A2)
    print("s1=",s1,"s2=",s2,"|s1-s2|=",abs(s1-s2))